<?php
class Recommendation_Popular_Friends extends Recommendation{
	
	public function __construct(){

		parent::__construct();
		$this->method_recommendation= RECOMMANDATION_POPULAIRE_AMIS;
	}
	
	protected function makeQuery(){
		
		$id_user_clause= "INNER JOIN PopularityFriends ON PopularityFriends.id_title=Title.id_title INNER JOIN FriendsFacebook ON id_user2=PopularityFriends.id_user  AND id_user1='".$_SESSION['infosAuth']->getIdUser()."' LEFT JOIN Rating ON Title.id_title=Rating.id_title AND Rating.id_user='".$_SESSION['infosAuth']->getIdUser()."' WHERE Rating.id_title is NULL ";
		
		/* Prérequete */
		$query = "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM Title ".$id_user_clause;
		
		$db = Outils_Bd::getInstance()->getConnexion();
		$sth=$db->prepare($query);
		$sth->execute();
		$offset = $sth->fetch(PDO::FETCH_ASSOC);

		$requete = "SELECT Title.id_title, Title.name, Title.artist, Title.youtube_id FROM Title ".$id_user_clause." LIMIT ".$offset['offset'].", 1"; 
		
	
		
		return $requete;
	}
	
	public static function canBeInstanciate(){
		if ( !isset($_SESSION['infosAuth']) || empty($_SESSION['infosAuth']) )
			return false;
		if (Friend_Bd::count("WHERE id_user=".$_SESSION['infosAuth']->getIdUser()) < 1)
			return false;
		return true;
	}
	
	public function getMethodRecommendation(){
		return $this->method_recommendation;
	}
	
}
?>